package com.hema.auction.widget.view;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.hema.auction.R;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * author by john
 * 封装的RecyclerView，初始化部分基本参数
 */
public class RefreshRecyclerView extends LinearLayout {

    @BindView(R.id.refresh_layout)
    SwipeRefreshLayout refreshLayout;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    LinearLayoutManager layoutManager;
    private HorizontalDividerItemDecoration horizontalDividerItemDecoration;
    private SwipeRefreshLayout.OnRefreshListener onRefreshListener;

    public RefreshRecyclerView(Context context) {
        super(context);
        init(context);
    }

    public RefreshRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public RefreshRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context) {
        ButterKnife.bind(this, LayoutInflater.from(context).inflate(R.layout.layout_refresh_recyclerview, this));
        refreshLayout.setColorSchemeResources(R.color.colorPrimary);
//        horizontalDividerItemDecoration = new HorizontalDividerItemDecoration.Builder(context)
//                .colorResId(R.color.colorDivider).showLastDivider()
//                .size(1).build();
//        recyclerView.addItemDecoration(horizontalDividerItemDecoration);
    }

    public void setLayoutManager(RecyclerView.LayoutManager layoutManager) {
        recyclerView.setLayoutManager(layoutManager);
    }

    public void setVerLayoutManager() {
        layoutManager = new LinearLayoutManager(getContext());
        ((LinearLayoutManager) layoutManager).setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
    }

    public void setHorLayoutManager() {
        layoutManager = new LinearLayoutManager(getContext());
        ((LinearLayoutManager) layoutManager).setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(layoutManager);
    }


    public void setGridLayoutManager(int spanCount) {
        layoutManager = new GridLayoutManager(getContext(), spanCount);
        recyclerView.setLayoutManager(layoutManager);
    }

    public void addItemDecoration() {

    }

    public void showLastDivider() {
        horizontalDividerItemDecoration = new HorizontalDividerItemDecoration.Builder(getContext())
                .colorResId(R.color.colorDivider).showLastDivider()
                .size(1).build();
        recyclerView.addItemDecoration(horizontalDividerItemDecoration);
    }

    public void removeItemDecoration() {
        recyclerView.removeItemDecoration(horizontalDividerItemDecoration);
    }

    public void setAdapter(BaseQuickAdapter adapter) {
        recyclerView.setAdapter(adapter);
    }

    public void addItemDecoration(RecyclerView.ItemDecoration itemDecoration) {
        recyclerView.addItemDecoration(itemDecoration);
    }

    public void addOnItemTouchListener(OnItemClickListener clickListener) {
        recyclerView.addOnItemTouchListener(clickListener);
    }

    public void setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener listener) {
        this.onRefreshListener = listener;
        refreshLayout.setOnRefreshListener(onRefreshListener);
    }

    public void setRefresh(boolean refreshing) {
        post(() -> {
            refreshLayout.setRefreshing(refreshing);
            if (onRefreshListener != null && refreshing) {
                recyclerView.scrollToPosition(0);
                onRefreshListener.onRefresh();
            }
        });
    }

    public RecyclerView getRecyclerView(){
        return recyclerView;
    }

    public boolean isRefreshing() {
        return refreshLayout.isRefreshing();
    }

    public void scrollToTop(){
        layoutManager.scrollToPosition(0);
    }

    /**
     * 判断是否滚动到顶部
     *
     * @return
     */
    public boolean isScrollTop() {
//        if (layoutManager instanceof GridLayoutManager) {
//            if (((GridLayoutManager) layoutManager).findFirstCompletelyVisibleItemPosition() == 0)
//                return true;
//            else
//                return false;
//        }
        if (layoutManager.findFirstCompletelyVisibleItemPosition() == 0)
            return true;
        else
            return false;
    }

}
